697C - Lorenzo Von Matterhorn - CodeForces Solution


brute force data structures implementation trees *1500

Please click on ads to support us..

C++ Code:

#include "bits/stdc++.h"
#include <cmath>
using namespace std;
#define EPS 1e-9
#define endl "\n"
#define clr(d,v) memset(d,v,sizeof(d))
#define ll long long
#define PI 3.1415926535
int dx[8] = {0,1,0,-1,1,-1,1,-1};
int dy[8] = {1,0,-1,0,1,1,-1,-1};
ll gcd(ll x, ll y) {return(!y) ? x : gcd(y, x%y);}
ll lcm(ll x, ll y) {return((x / gcd(x, y))*y);}
void fast();
const int N = 2e5 + 5;
ll mod = 1e9+7;
//--------------------------------------------------------
ll fact[25];
map<pair<ll, int>,ll> up;
map<pair<ll, ll>, ll> val;
//--------------------------------------------------------
ll get_lca(ll a, ll b){
    map<ll,bool> mp;
    while(a){
        mp[a] = 1;
        a = a / 2;
    }
    while(b){
        if(mp[b]) return b;
        b = b / 2;
    }
    return 1;
}

void update(ll node, ll lca, ll w){
    while(node != lca){
        val[{node / 2, node}] += w;
        node /= 2;
    }
}

ll get(ll node, ll lca){
    ll sum = 0;
    while(node != lca){
        sum += val[{node / 2,node}];
        node /= 2;
    }
    return sum;
}

//--------------------------------------------------------
int main(){
    
    fast();

    int q;
    cin >> q;
    set<ll> nodes;
    vector<pair<pair<ll, ll>, pair<ll, ll>>> vec;
    for(int i = 0 ; i < q ; i++){
        ll t,u,v,w=0;
        cin >> t;
        if(t==2) cin >> u >> v;
        else cin >> u >> v >> w;
        vec.push_back({{t,u},{v,w}});
        nodes.insert(u);
        nodes.insert(v);
    }
    

    
//
    for(int i = 0 ; i < vec.size() ; i++){
        ll t = vec[i].first.first;
        ll u = vec[i].first.second;
        ll v = vec[i].second.first;
        ll w = vec[i].second.second;
        ll lca = get_lca(u, v);
        if(t == 1){
            update(u,lca,w);
            update(v,lca,w);
        }
        else{
//            cout << lca  << endl;
           cout << get(u,lca) + get(v,lca)<< endl;
        }
//
//        for(int j = 1 ; j <= 10 ; j++){
//           // cout << j << "-" << val[j] << "   ";
//        }
//        cout << endl;
    }

    
    

    
    
}
//---------------------------------------------------------
    
void fast(){
    std::ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}

 	 	 			  					 	  					     		


Comments

Submit
0 Comments
More Questions

963A - Alternating Sum
1191B - Tokitsukaze and Mahjong
1612G - Max Sum Array
1459B - Move and Turn
1006F - Xor-Paths
706C - Hard problem
304C - Lucky Permutation Triple
1301C - Ayoub's function
38E - Let's Go Rolling
171G - Mysterious numbers - 2
1183C - Computer Game
400C - Inna and Huge Candy Matrix
417A - Elimination
222A - Shooshuns and Sequence
1736A - Make A Equal to B
1736B - Playing with GCD
887C - Solution for Cube
1737C - Ela and Crickets
1741C - Minimize the Thickness
1741A - Compare T-Shirt Sizes
1741D - Masha and a Beautiful Tree
109B - Lucky Probability
1741B - Funny Permutation
1741E - Sending a Sequence Over the Network
344B - Simple Molecules
370A - Rook Bishop and King
546E - Soldier and Traveling
1741G - Kirill and Company
1200B - Block Adventure
1088B - Ehab and subtraction